clear all
set more off

global data 	"R:\SharedProjects\Shared2020-070\2016\extend_to_2020\JPE_Replication_dta"
global in 		"R:\SharedProjects\Shared2020-070\2016\input"
global figures 	"R:\SharedProjects\Shared2020-070\2016\extend_to_2020\JPE_Replication_log"

cap log close
log using $figures\G_file,replace t


cd $data

*************************************************************************************************************************************************
u complete_data_final_v1.dta, clear

gen cantwork_notemp_appl=hlthlm_appl==1 & cantwork_appl==1 & hlth_temp_appl==0			/* This is the (stricter) definition used in the paper */
gen someworklim			=hlthlm_appl==1													/* This is the conventional 0-1 binary indicator*/

bysort hhidpn cycleid rid: egen success_cycle = total(success)
replace success_cycle = 1 if success_cycle >0 & success_cycle!=.
replace success_cycle = 0 if success_cycle!=1
gen nosuccess_cycle = 1-success_cycle

keep if round==1 						// keep only first round of an application cycle (and hence only people with F831 records)
keep if year==year_app 					// only keep observation from application year

** Define sample
keep if within_threshold_appl==1 		// only keep if HRS interview within 12 months of application
drop if (age<20 | age>65) & age!=. 		// keep only those in relevant age range
drop if proxy_appl == 1 				// drop proxy interviews

***Combine some occupations due to small cell size
replace longest_occ_combined_appl=6 if longest_occ_combined_appl==17	/*Military ->Protective Services*/
replace longest_occ_combined_appl=9 if longest_occ_combined_appl==5		/*prv hhld/clean -> Food prep services*/

tab bs, gen(bsx)														/*Body system code from F831*/
tab longest_occ_combined_appl, gen(longestoccx)

gen 	occ_interm=.
replace occ_interm=1 if longest_occ_combined_appl==0									/*Unknown*/
replace occ_interm=2 if longest_occ_combined_appl>=1  & longest_occ_combined_appl<=2	/*Manag/Prof*/
replace occ_interm=3 if longest_occ_combined_appl>=3  & longest_occ_combined_appl<=4	/*Sales/clerical*/
replace occ_interm=4 if longest_occ_combined_appl>=5  & longest_occ_combined_appl<=7	/*Clean/Protect/Food serv*/
replace occ_interm=5 if longest_occ_combined_appl>=8  & longest_occ_combined_appl<=9	/*Personal/Health serv*/
replace occ_interm=6 if longest_occ_combined_appl>=10 & longest_occ_combined_appl<=12	/*Farm/Constr/Mech*/
replace occ_interm=7 if longest_occ_combined_appl==13 | longest_occ_combined_appl==17	/*Precision/Armed force*/
replace occ_interm=8 if longest_occ_combined_appl>=14 & longest_occ_combined_appl<=16	/*operators*/

tab raracem, gen(racex)

gen doctor_told_has_hbp=bs_sf_appl==1
gen doctor_told_has_psy=bs_sf_appl==2
gen doctor_told_has_hea=bs_sf_appl==3
gen doctor_told_has_art=bs_sf_appl==4
gen doctor_told_has_dia=bs_sf_appl==5
gen doctor_told_has_lun=bs_sf_appl==6
gen doctor_told_has_str=bs_sf_appl==7
gen doctor_told_has_can=bs_sf_appl==8

replace bmi_appl=. if bmi_appl>300

gen 	sample=0
replace sample=1 if success!=. & nosuccess!=. & rid!=. & hlthlm_appl!=. & college!=. & raracem!=. & married_appl!=. & widowed_appl!=. ///
					& experience!=. & age!=. &  female!=.  & bs!=.  & bmi_appl!=.

gen 	type1=.
replace type1=0 if success==1 & cantwork_notemp_appl==1
replace type1=1 if success==0 & cantwork_notemp_appl==1
gen 	type2=.
replace type2=0 if success==0 & cantwork_notemp_appl==0
replace type2=1 if success==1 & cantwork_notemp_appl==0
lab var type1 "Type I error, appl. round"
lab var type2 "Type II error, appl. round"
bysort female: egen type1sh = mean(type1)
bysort female: egen type2sh = mean(type2)

gen 	type1_cycle=.
replace type1_cycle=0 if success_cycle==1 & cantwork_notemp_appl==1
replace type1_cycle=1 if success_cycle==0 & cantwork_notemp_appl==1
gen 	type2_cycle=.
replace type2_cycle=0 if success_cycle==0 & cantwork_notemp_appl==0
replace type2_cycle=1 if success_cycle==1 & cantwork_notemp_appl==0
lab var type1_cycle "Type I error, appl. cycle"
lab var type2_cycle "Type II error, appl. cycle"
bysort female: egen type1sh_cycle = mean(type1_cycle)
bysort female: egen type2sh_cycle = mean(type2_cycle)

sum type1 if female==1
label var type1sh "Type I error, 1st appl. round"
label var type2sh "Type II error, 1st appl. round"
label var type1sh_cycle "Type I error, appl. cycle"
label var type2sh_cycle "Type II error, appl. cycle"
label var success "Awarded"
label var success_cycle "Awarded"
label var nosuccess "Denial, appl. round"
label var nosuccess_cycle "Denial, appl. cycle"

label var cantwork_notemp_appl "Health lmtn"
label var college "College degree"
label var white White
label var married_appl "Married"
label var widowed_appl "Widowed"
label var experience "Lab. mark. experience"
label var age Age
label var female Female
label var ssi "Applies for SSI"
lab def female 0 "Male" 1 "Female", replace
lab val female female
label var success 			"Awarded"
label var nosuccess 		"No award"
label var nosuccess_cycle 	"No award"
label var success_cycle 	"Awarded"
label var racex2 "Black"
label var racex3 "Other race"

g ssi_only=rid==16
g di_only=rid==2
g ssi_and_di=rid==216

label var ssi_only "Applied SSI only"
label var ssi_and_di "Applied DI and SSI"

qui tab year,gen(yrd)

* ADL recode
foreach i in walkra_appl dressa_appl stoopa_appl beda_appl hosp_appl {
	gen `i'_1 = 0
	replace `i'_1 = 1 if `i' == 1
}

tab occ_interm,gen(occx)

gen 	year_type12_reg=year
replace year_type12_reg=1994 if year==1995		/*join 1994-1995 b/c too few obs in the {L=0,R=1} group (confidentiality issues)*/
drop yrd*
tab year_type12_reg,gen(yrd)

*************************************************
keep if sample==1
*************************************************

save $data\sampleA,replace
***************************************************************************************************************************************************
***************************************************************************************************************************************************


cd $data
u complete_data_final_2_12_v1.dta, clear

gen 	cantwork_notemp_appl = hlthlm_appl==1 & cantwork_appl==1 & hlth_temp_appl==0			/* This is the (stricter) definition used in the paper */
keep if round==1 						// keep only first round of an application cycle (and hence only people with F831 records)
keep if year==year_app 					// only keep observation from application year

** Define sample
keep if within_threshold_appl==1 		// only keep if HRS interview within 12 months of application
drop if (age<20 | age>65) & age!=. 		// keep only those in relevant age range
drop if proxy_appl == 1 				//drop proxy interviews

***Combine some occupations due to small cells
replace longest_occ_combined_appl=6 if longest_occ_combined_appl==17	/*Military ->Protective Services*/
replace longest_occ_combined_appl=9 if longest_occ_combined_appl==5		/*prv hhld/clean -> Food prep services*/

tab bs, gen(bsx)														/*Body system code from F831*/
tab longest_occ_combined_appl, gen(longestoccx)
tab raracem, gen(racex)

gen doctor_told_has_hbp=bs_sf_appl==1
gen doctor_told_has_psy=bs_sf_appl==2
gen doctor_told_has_hea=bs_sf_appl==3
gen doctor_told_has_art=bs_sf_appl==4
gen doctor_told_has_dia=bs_sf_appl==5
gen doctor_told_has_lun=bs_sf_appl==6
gen doctor_told_has_str=bs_sf_appl==7
gen doctor_told_has_can=bs_sf_appl==8

replace bmi_appl=. if bmi_appl>300

gen sample=0
replace sample=1 if success!=. & nosuccess!=. & rid!=. & hlthlm_appl!=. & college!=. & raracem!=. ///
& married_appl!=. & widowed_appl!=. & experience!=. & age!=. &  female!=.  & bs!=.  & bmi_appl!=.

keep if sample==1

g ssi_only=rid==16
g di_only=rid==2
g ssi_and_di=rid==216

* ADL recode
foreach i in walkra_appl dressa_appl stoopa_appl beda_appl hosp_appl {
	gen `i'_1 = 0
	replace `i'_1 = 1 if `i' == 1
}

qui tab year,gen(yrd)
save $data\sampleB,replace
***************************************************************************************************************************************************
***************************************************************************************************************************************************


***************************************************************************************************************************************************
***************************************************************************************************************************************************
cd $data
u $data\sampleA, clear

qui probit cantwork_notemp_appl bsx2 bsx3 bsx4 bsx5 bsx6 bsx7 bsx8 bsx9 bsx10 doctor* walkra_appl_1 dressa_appl_1 stoopa_appl_1 beda_appl_1 hosp_appl_1 bmi_appl 
predict ppp,pr
su ppp if success==1
scalar E_ppp_success=r(mean)
gen wd_alt_1=ppp>=E_ppp_success

qui probit cantwork_notemp_appl bsx2 bsx3 bsx4 bsx5 bsx6 bsx7 bsx8 bsx9 bsx10 doctor* walkra_appl_1 dressa_appl_1 stoopa_appl_1 beda_appl_1 hosp_appl_1 bmi_appl if female==1
predict pppf,pr
su pppf if success==1
scalar E_pppf_success=r(mean)
gen wd_alt_2=pppf>=E_pppf_success

gen obese1=bmi_appl>=30 & bmi_appl<.
gen overw1=bmi_appl>=25 & bmi_appl<30
gen underw1=bmi_appl<=18.5

qui mca bsx2 bsx3 bsx4 bsx5 bsx6 bsx7 bsx8 bsx9 bsx10 doctor* walkra_appl_1 dressa_appl_1 stoopa_appl_1 beda_appl_1 hosp_appl_1 obese1 underw1 overw1
predict pmca_1
su pmca_1 if success==1
scalar E_pmca_success=r(mean)
gen wd_alt_3=pmca>=E_pmca_success

keep hhidpn year wd_alt*
sort hhidpn year
save $data\wd_alt,replace
***************************************************************************************************************************************************
***************************************************************************************************************************************************



**************************************************************************************************
*** work after decision **************************************************************************
**************************************************************************************************
cd $data
u complete_data_final_v1, clear

sort hhidpn year
merge hhidpn year using $data\wd_alt
drop _merge

gen 	cantwork_notemp_appl = hlthlm_appl==1 & cantwork_appl==1 & hlth_temp_appl==0			/* This is the (stricter) definition used in the paper */

bysort hhidpn cycleid rid: egen success_cycle = total(success)
replace success_cycle = 1 if success_cycle >0 & success_cycle!=.
replace success_cycle = 0 if success_cycle!=1
gen nosuccess_cycle = 1-success_cycle

keep if round==1 											// keep only first round of an application cycle
keep if year_dec != . 										//keep only applicants (this drops no-one)
keep if (year - year_dec) <= 5 & (year - year_dec) >= 0		/*Work up to 5 years following year of decision*/
															

** Define sample
keep if within_threshold_appl==1 		// only keep if HRS interview within 12 months of application
drop if (age<20 | age>65) & age!=. 		// keep only those in relevant age range
drop if proxy_appl == 1 				// drop proxy interviews


***Combine some occupations due to small cell size
replace longest_occ_combined_appl=6 if longest_occ_combined_appl==17	/*Military ->Protective Services*/
replace longest_occ_combined_appl=9 if longest_occ_combined_appl==5		/*prv hhld/clean -> Food prep services*/

tab bs, gen(bsx)														/*Body system code from F831*/
tab longest_occ_combined_appl, gen(longestoccx)
tab raracem, gen(racex)

gen doctor_told_has_hbp=bs_sf_appl==1
gen doctor_told_has_psy=bs_sf_appl==2
gen doctor_told_has_hea=bs_sf_appl==3
gen doctor_told_has_art=bs_sf_appl==4
gen doctor_told_has_dia=bs_sf_appl==5
gen doctor_told_has_lun=bs_sf_appl==6
gen doctor_told_has_str=bs_sf_appl==7
gen doctor_told_has_can=bs_sf_appl==8

replace bmi_appl=. if bmi_appl>300


sort hhidpn year, stable
duplicates drop hhidpn year, force			/*Some individuals have multiple strings of records because they submit multiple applications. Since we'd be studying labor supply*/
											/*1 obs per individual suffices*/
xtset hhidpn year

gen F_ern_real  = F.earn_real
gen F2_ern_real = F2.earn_real
gen F3_ern_real = F3.earn_real

egen earn_3 = rowtotal(F_ern_real F2_ern_real F3_ern_real)
gen years_3 = (F_ern_real > 0 & F_ern_real != .) + (F2_ern_real > 0 & F2_ern_real != .) + (F3_ern_real > 0 & F3_ern_real != .) 		/*Number of years with usable earnings*/
gen avg_earn3 = earn_3/3
replace earn_3 = 1 if earn_3 > 0		/*This generates a dummy: 1=some earnings in the 3 years following application, 0=no earnings*/ 

gen F4_ern_real = F4.earn_real
gen F5_ern_real = F5.earn_real

egen earn_5 = rowtotal(F_ern_real F2_ern_real F3_ern_real F4_ern_real F5_ern_real)
replace earn_5 = 1 if earn_5 > 0		/*This generates a dummy: 1=some earnings in the 5 years following application, 0=no earnings*/

** generate employed only if earned more than sga
merge m:1 year using $in\sga
drop if _merge == 2 
gen sga_real = sga/(cpi/100)
gen earned_sga = earn_real >= sga_real & earn_real != .		/*dummy for earnings above SGA*/
sort hhidpn year, stable
forvalues l = 1(1)5 {
	gen F`l'_earned_sga = F`l'.earned_sga
}

egen earn_sga_3 = rowtotal(F1_earned_sga F2_earned_sga F3_earned_sga)
replace earn_sga_3 = 1 if earn_sga_3 > 0		/*Any positive earnings above SGA in the 3 years after decision*/
egen earn_sga_5 = rowtotal(F1_earned_sga F2_earned_sga F3_earned_sga F4_earned_sga F5_earned_sga)
replace earn_sga_5 = 1 if earn_sga_5 > 0		/*Any positive earnings above SGA in the 5 years after decision*/

keep if year == year_dec						/*Sample*/
gen lavg_ern = log(1+avg_earn3)

label var cantwork_notemp_appl "Disabled"

g rejected= nosuccess
g limited =cantwork_notemp_appl

g rejected_limited= cantwork_notemp_appl*nosuccess
g rejected_notlimited= (1-cantwork_notemp_appl)*nosuccess
g awarded_limited= cantwork_notemp_appl*(1-nosuccess)
g awarded_notlimited= (1-cantwork_notemp_appl)*(1-nosuccess)

g ssi_only=rid==16
g di_only=rid==2
g ssi_and_di=rid==216

qui reghdfe earn_sga_3 rejected rejected_limited cantwork_notemp_appl female college racex2 experience ///
						doctor* ssi_only ssi_and_di married_appl widowed_appl age ///
						walkra_appl dressa_appl stoopa_appl beda_appl hosp_appl bmi_appl ///
						, vce(cluster hhidpn) abs(year longest_occ_combined_appl bs)
keep if e(sample)

g rejectedfem=rejected*female
gen femalelim=female*cantwork_notemp_appl

lab var rejected "Rejected"
lab var female "Female"
lab var cantwork_notemp_appl "Work disabled"
lab var femalelim "Female x Work disabled"
lab var rejectedfem "Female x Rejected"
lab var rejected_limited "Rejected x Work disabled"

save $data\sampleA_labor_future,replace 
***************************************************************************************
***************************************************************************************



***************************************************************************************
*** work before decision
***************************************************************************************
cd $data
u complete_data_final_v1, clear

gen 	cantwork_notemp_appl = hlthlm_appl==1 & cantwork_appl==1 & hlth_temp_appl==0			/* This is the (stricter) definition used in the paper */

bysort hhidpn cycleid rid: egen success_cycle = total(success)
replace success_cycle = 1 if success_cycle >0 & success_cycle!=.
replace success_cycle = 0 if success_cycle!=1
gen nosuccess_cycle = 1-success_cycle

keep if round==1 // keep only first round of an application cycle
keep if year_dec != . //keep only applicants
keep if (year - year_dec) <= 5 & (year - year_dec) >= -11

** Define sample
keep if within_threshold_appl==1 	// only keep if HRS interview within 12 months of application
drop if (age<20 | age>65) & age!=. 	// keep only those in relevant age range
drop if proxy_appl == 1 			//drop proxy interviews

replace longest_occ_combined_appl=6 if longest_occ_combined_appl==17	/*Military ->Protective Services*/
replace longest_occ_combined_appl=9 if longest_occ_combined_appl==5		/*prv hhld/clean -> Food prep services*/

tab bs, gen(bsx)														/*Body system code from F831*/
tab longest_occ_combined_appl, gen(longestoccx)
tab raracem, gen(racex)

gen doctor_told_has_hbp=bs_sf_appl==1
gen doctor_told_has_psy=bs_sf_appl==2
gen doctor_told_has_hea=bs_sf_appl==3
gen doctor_told_has_art=bs_sf_appl==4
gen doctor_told_has_dia=bs_sf_appl==5
gen doctor_told_has_lun=bs_sf_appl==6
gen doctor_told_has_str=bs_sf_appl==7
gen doctor_told_has_can=bs_sf_appl==8

replace bmi_appl=. if bmi_appl>300

sort hhidpn year, stable
duplicates drop hhidpn year, force			/*Some individuals have multiple strings of records because they submit multiple applications. Since we're studying labor supply*/
											/*1 obs per individual suffices*/
xtset hhidpn year

** generate employed only if earned more than sga
merge m:1 year using $in\sga
drop if _merge == 2 
gen sga_real = sga/(cpi/100)
gen earned_sga = earn_real >= sga_real & earn_real != .
sort hhidpn year, stable

//Comparison with Maestas et al
g y=(cpi/100)*earn_real
g y_real=y/(cpi/113.987) 	/*in 2008 $*/
forvalues i = 3(1)5 {
	gen y_r_L`i' = L`i'.y_real
}
egen 	y_past_3_5 = rowtotal(y_r_L3-y_r_L5)
replace y_past_3_5=y_past_3_5/3
su 		y_past_3_5 if year==year_dec & rid!=16
drop y y_real y_r_L* y_past_3_5

// study earnings before decision

forvalues i = 3(1)10 {
	gen ern_r_L`i' = L`i'.earn_real
}

egen earn_past_3_5 = rowtotal(ern_r_L3-ern_r_L5)
replace earn_past_3_5=earn_past_3_5/3					/*Average earnings over the t-3 to t-5 period*/

egen earn_past_5_10 = rowtotal(ern_r_L5-ern_r_L10)
replace earn_past_5_10 = 1 if earn_past_5_10 > 0		/*Dummy for positive earnings over the t-5 to t-10 period*/

forvalues i = 5(1)10 {
	gen earned_sga_L`i' = L`i'.earned_sga
}

egen earn_past_sga = rowtotal(earned_sga_L5-earned_sga_L10)	
replace earn_past_sga = 1 if earn_past_sga > 0			/*Dummy for earnings above SGA*/

keep if year == year_dec

label var cantwork_notemp_appl "Disabled"

g rejected_limited= cantwork_notemp_appl*nosuccess
g rejected= nosuccess
g ssi_only=rid==16
g di_only=rid==2
g ssi_and_di=rid==216
g rejectedfem=rejected*female
gen femalelim=female*cantwork_notemp_appl

lab var rejected "Rejected"
lab var female "Female"
lab var cantwork_notemp_appl "Work disabled"
lab var femalelim "Female x Work disabled"
lab var rejectedfem "Female x Rejected"
lab var rejected_limited "Rejected x Work disabled"

qui reghdfe earn_past_sga rejected rejected_limited cantwork_notemp_appl female college racex2 experience  ///
						doctor* ssi_only ssi_and_di married_appl widowed_appl age ///
						walkra_appl dressa_appl stoopa_appl beda_appl hosp_appl bmi_appl ///
						 , vce(cluster hhidpn) abs(year bs longest_occ_combined_appl)

keep if e(sample)

save $data\sampleA_labor_past,replace 
***************************************************************************************
***************************************************************************************



cd $data
u complete_data_final_v1.dta, clear

** Define sample *****************
keep if year==interview_year
duplicates drop hhidpn wave, force		/*The duplicates here are people who have multiple f831 records. */
										/*Now these are only HRS records*/
drop if (age<20 | age>65) & age!=. 		// keep only those in relevant age range
drop if proxy_intvw == 1 				// drop proxy interviews
keep if received_ssidi==0				/*keep those who are not receiving SSI/DI*/
drop if year_died<=2006	
**********************************

g cantwork_notemp=hlthlm==1 & cantwork==1 & hlth_temp==0			/* This is the (stricter) definition used in the paper */

tab longest_occ_combined_intvw, gen(longestoccx)
tab raracem, gen(racex)

* ADL recode
foreach i in walkra_intvw dressa_intvw stoopa_intvw beda_intvw hosp_intvw {
	gen `i'_1 = 0
	replace `i'_1 = 1 if `i' == 1
}

replace bmi_intvw=. if bmi_intvw>300
g cantwork_notemp_intvw=hlthlm_intvw==1 & cantwork_intvw==1 & hlth_temp_intvw==0		/* This is the (stricter) definition used in the paper */

g occ_interm=.
replace occ_interm=1 if longest_occ_combined_intvw==0									/*Unknown*/
replace occ_interm=2 if longest_occ_combined_intvw>=1  & longest_occ_combined_intvw<=2	/*Manag/Prof*/
replace occ_interm=3 if longest_occ_combined_intvw>=3  & longest_occ_combined_intvw<=4	/*Sales/clerical*/
replace occ_interm=4 if longest_occ_combined_intvw>=5  & longest_occ_combined_intvw<=7	/*Clean/Protect/Food serv*/
replace occ_interm=5 if longest_occ_combined_intvw>=8  & longest_occ_combined_intvw<=9	/*Personal/Health serv*/
replace occ_interm=6 if longest_occ_combined_intvw>=10 & longest_occ_combined_intvw<=12	/*Farm/Constr/Mech*/
replace occ_interm=7 if longest_occ_combined_intvw==13 | longest_occ_combined_intvw==17	/*Precision/Armed force*/
replace occ_interm=8 if longest_occ_combined_intvw>=14 & longest_occ_combined_intvw<=16	/*operators*/

gen doctor_told_has_hbp=bs_sf_intvw==1
gen doctor_told_has_psy=bs_sf_intvw==2
gen doctor_told_has_hea=bs_sf_intvw==3
gen doctor_told_has_art=bs_sf_intvw==4
gen doctor_told_has_dia=bs_sf_intvw==5
gen doctor_told_has_lun=bs_sf_intvw==6
gen doctor_told_has_str=bs_sf_intvw==7
gen doctor_told_has_can=bs_sf_intvw==8

replace astck_intvw_real=0 if astck_intvw_real<0
gen logbs=log(itot_intvw_real+achck_intvw_real+astck_intvw_real+acd_intvw_real+abond_intvw_real)

global spec_A 		female college racex2 experience doctor* married widowed age i.wave ///
					walkra_intvw_1 dressa_intvw_1 stoopa_intvw_1 beda_intvw_1 hosp_intvw_1 bmi_intvw i.occ_interm logbs 

sort hhidpn year
merge hhidpn year using temp_applied				/*This dataset classifies people in round-1 application status (current or still waiting in that particular year)*/
tab _merge
drop if _merge==2
drop _merge
gen 	applied_round1=applied_c+applied_w
replace applied_round1=0 if applied_round1==.
					
qui reg applied_round1 $spec_A
keep if e(sample)
				
save $data\sampleC,replace




cd $data
u complete_data_final_v1.dta, clear

g cantwork_notemp_appl=hlthlm_appl==1 & cantwork_appl==1 & hlth_temp_appl==0			/* This is the (stricter) definition used in the paper */

keep if round==1 						// keep only first round of an application cycle (and hence only people with F831 records)
keep if year==year_app 					// only keep observation from application year

** Define sample
keep if within_threshold_appl==1 		// only keep if HRS interview within 12 months of application
drop if (age<20 | age>65) & age!=. 		// keep only those in relevant age range
drop if proxy_appl == 1 		

replace bmi_appl=. if bmi_appl>300
gen sample=0
replace sample=1 if success!=. & nosuccess!=. & rid!=. & hlthlm_appl!=. & college!=. & ///
raracem!=. & married_appl!=. & widowed_appl!=. & experience!=. & age!=. &  female!=.  & bs!=.  & bmi_appl!=.

gen months_to_int = floor(days_to_interview_appl/31) + 1
keep hhidpn year cantwork_notemp_appl months_to_int days_to_interview_appl date_app sample
tempfile 12moa
save `12moa'

u complete_data_final_12mb_v1.dta, clear

g cantwork_notemp_appl=hlthlm_appl==1 & cantwork_appl==1 & hlth_temp_appl==0			/* This is the (stricter) definition used in the paper */

keep if round==1 						// keep only first round of an application cycle (and hence only people with F831 records)
keep if year==year_app 					// only keep observation from application year

** Define sample
keep if within_threshold_appl==1 		// only keep if HRS interview within 12 months of application
drop if (age<20 | age>65) & age!=. 		// keep only those in relevant age range
drop if proxy_appl == 1 		

replace bmi_appl=. if bmi_appl>300
gen sample=0
replace sample=1 if success!=. & nosuccess!=. & rid!=. & hlthlm_appl!=. & college!=. & ///
raracem!=. & married_appl!=. & widowed_appl!=. & experience!=. & age!=. &  female!=.  & bs!=.  & bmi_appl!=.

drop if days_to_interview_appl==0			/*These are allocated to the baseline file, so are pure duplicates here*/

gen months_to_int = floor((days_to_interview_appl)/31) + 1
replace months_to_int = - months_to_int
keep hhidpn year cantwork_notemp_appl months_to_int days_to_interview_appl date_app sample
append using `12moa'

keep if sample==1
save $data\sampleA_extended,replace

erase temp_applied.dta
erase wd_alt.dta

log close
